package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Compensation;
import cn.wolfcode.crm.domain.Task;
import cn.wolfcode.crm.query.CompensationQueryObject;
import cn.wolfcode.crm.query.TaskQueryObject;
import cn.wolfcode.crm.service.ICompensationService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.print.attribute.standard.Compression;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * Created by Administrator on 2018/6/12.
 */

@Controller
@RequestMapping("compensation")
public class CompensationController {

    @Autowired
    private ICompensationService compensationService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"compensation:view", "薪资页面"}, logical = Logical.OR)
    public String view() {
        return "compensation";
    }


    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"task:query", "薪资列表"}, logical = Logical.OR)
    public PageResult query(CompensationQueryObject qo) {
        return compensationService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"task:saveOrUpdate", "员工新增/更改"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Compensation compensation) {
        try {
            if (compensation.getId() != null) {
                compensationService.updateByPrimaryKey(compensation);
            } else {
                //保存新的员工要设置默认的状态为在职
                compensation.setStatus(true);
                compensationService.insert(compensation);
            }
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"compensation:changeState", "员工新增/更改"}, logical = Logical.OR)
    public JsonResult changeState(Compensation compensation) {
        try {
            if (compensation.getId() != null) {
                compensation.setStatus(true);
                compensationService.changeState(compensation);
            }
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response, TaskQueryObject qo) throws IOException {
        response.setHeader("Content-disposition", "attachment;filename=task.xls");
        //创建excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("sheet01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("部门");
        row.createCell(3).setCellValue("联系电话");
        row.createCell(4).setCellValue("邮箱");
        row.createCell(5).setCellValue("基本工资");
        row.createCell(6).setCellValue("奖金");
        row.createCell(7).setCellValue("总薪资");
        row.createCell(8).setCellValue("状态");
        List<Compensation> list = compensationService.selectAll();
        for (int i = 0; i < list.size(); i++) {
            Compensation compensation = list.get(i);
            row = sheet.createRow(i + 1);
            if(compensation.getDate()!=null){
                CreationHelper createHelper = workbook.getCreationHelper();
                CellStyle cellStyle = workbook.createCellStyle();
                cellStyle.setDataFormat(
                        createHelper.createDataFormat().getFormat("m/d/yy"));
                HSSFCell cellDate = row.createCell(0);
                cellDate.setCellStyle(cellStyle);
                cellDate.setCellValue(compensation.getDate());
            }else {
                row.createCell(0).setCellValue("");
            }
            row.createCell(1).setCellValue(compensation.getEmployee().getUsername());
            row.createCell(2).setCellValue(compensation.getDept().getName());
            row.createCell(3).setCellValue(compensation.getEmployee().getTel());
            row.createCell(4).setCellValue(compensation.getEmployee().getEmail());
            if (compensation.getBasicMoney()!=null){
                row.createCell(5).setCellValue(compensation.getBasicMoney());
            }else {
                row.createCell(5).setCellValue("");
            }
            if (compensation.getBonus()!=null){
                row.createCell(6).setCellValue(compensation.getBonus());
            }else {
                row.createCell(6).setCellValue("");
            }
            if (compensation.getSumMoney()!=null){
                row.createCell(7).setCellValue(compensation.getSumMoney());
            }else {
                row.createCell(7).setCellValue("");
            }
            row.createCell(8).setCellValue(compensation.getStatus()? "在职":"离职");
        }
        workbook.write(response.getOutputStream());
    }


}